# This code is used to estimate average delegation, constraint and discretion indices by year

library(ggplot2)
library(foreign)

data = 
  read.csv("~/Dropbox/Research/Papers/Delegation-ML-Project/FINAL-OUTPUT-DATA/APSR_RnR/discretion-delegation-constraint_COM.csv")

########### Commission data Now average by year com data

ag2<-aggregate(data, by=list(data$year), FUN=mean, na.rm=TRUE)


ones<-rep(1,dim(data)[1])

ob<-data.frame(ones,data$year)

Ns<-aggregate(ob, by = list(data$year), FUN = sum)

NLaws<-Ns$ones # This is the number of laws collected per year

## Get the standard errors of the ratios

sd.delegation<-aggregate(data$delegation,by = list(data$year), FUN = sd,na.rm=TRUE)
var.delegation<-sd.delegation$x^2
se.delegation<- sd.delegation$x/sqrt(NLaws)

sd.constraint.ms<-aggregate(data$constraint.ratio,by = list(data$year), FUN = sd,na.rm=TRUE)
var.constraint.ms<-sd.constraint.ms$x^2
se.constraint.ms<- sd.constraint.ms$x/sqrt(NLaws)

sd.discretion<-aggregate(data$discretion.index,by = list(data$year), FUN = sd,na.rm=TRUE)
var.discretion<-sd.discretion$x^2
se.discretion<- sd.discretion$x/sqrt(NLaws)


## Now let's add all of this information to the aggregated data
ag2<-data.frame(ag2,var.delegation,se.delegation,
                var.constraint.ms,se.constraint.ms,
                var.discretion,se.discretion)

EU.Laws<-NLaws
################################################################################
################################               #################################
################################  MAIN PLOTS   #################################
################################               #################################
################################################################################

setwd("~/Dropbox/Research/Papers/Delegation-ML-Project/Draft-EU-Paper/figs/APSR_RnR")

## Plot number of laws collected by  year
ggplot(ag2, aes(x=year, y=EU.Laws)) + geom_point(shape=1)+
  geom_line()+theme_classic() + ylab("EU Laws Collected") + 
  xlab("Year") +  scale_x_continuous(breaks = seq(1959, 2017, by = 2)) + 
  theme(axis.text.x = element_text(angle = 70, hjust = 1))

ggsave("nlaws_FINAL.png")

# Restrict data to post 1963 for illustration
ag2new = ag2[ag2$year>=1975,]

## Delegation only
ggplot(ag2new, aes(x=year, y=delegation.ratio)) +
  geom_errorbar(aes(ymin=delegation.ratio-1.96*se.delegation, 
                    ymax=delegation.ratio+1.96*se.delegation),width=0.5 
                ,linetype= "dotted") +  theme_classic() + geom_point() + 
  geom_line() +
  scale_x_continuous(breaks = seq(1965, 2017, by = 2)) + 
  theme(axis.text.x = element_text(angle = 70, hjust = 1),
        legend.title = element_blank()) +
  xlab("Year") + ylab("Mean Delegation Ratio (Commission)") +   geom_smooth()
#+expand_limits(y = c(0.01,0.07))

ggsave("delegation-only_FINAL_COM.png")


## Constraint only
ggplot(ag2new, aes(x=year, y=constraint.ratio)) +
  geom_errorbar(aes(ymin=constraint.ratio-1.96*se.constraint.ms, 
                    ymax=constraint.ratio+1.96*se.constraint.ms),width=0.5 
               , linetype = "dotted") +  theme_classic() + geom_point() + 
  geom_line() +
  scale_x_continuous(breaks = seq(1965, 2017, by = 2)) + 
  theme(axis.text.x = element_text(angle = 70, hjust = 1),
        legend.title = element_blank()) +
  xlab("Year") + ylab("Mean Constraint Ratio (Commission)")  +   geom_smooth()
#+  expand_limits(y = c(0,0.05))

ggsave("constraint-only_FINAL_COM.png")



## Discretion index
ggplot(ag2new, aes(x=year, y=discretion.index)) +
  geom_errorbar(aes(ymin=discretion.index-1.96*se.discretion, 
                    ymax=discretion.index+1.96*se.discretion),width=0.5, 
                 linetype = "dotted") +  theme_classic() + geom_point() + 
  geom_line() +
  scale_x_continuous(breaks = seq(1965, 2017, by = 2)) + 
  theme(axis.text.x = element_text(angle = 70, hjust = 1),
        legend.title = element_blank()) +
  xlab("Year") + ylab("Mean Discretion Index (Commission)")  + geom_smooth(method = lm,se=FALSE, formula = y ~ splines::bs(x, 3)) + 
  expand_limits(y = c(0.00,0.06))

ggsave("discretion-only_FINAL_COM.png")



### reconstruct PDR and PCR
library(devtools)
#devtools::install_github("MatthieuStigler/RDDtools/RDDtools")
library(RDDtools)
rm(data)

data = read.csv("/Users/jason/Dropbox/Research/Papers/Delegation-ML-Project/FINAL-OUTPUT-DATA/APSR_RnR/final_delegation_data_rnr_com.csv")

names(data)
#[1] "X"                                     "newprovision.celex"                    "newprovisiondata.month"               
#[4] "newprovision.year"                     "com.consultation.requirements.probs"   "com.consultation.requirements.preds"  
#[7] "com.executive.action.possible.probs"   "com.executive.action.possible.preds"   "com.executive.action.required.probs"  
#[10] "com.executive.action.required.preds"   "com.legislative.action.possible.probs" "com.legislative.action.possible.preds"
#[13] "com.public.hearings.probs"             "com.public.hearings.preds"             "com.reporting.requirements.probs"     
#[16] "com.reporting.requirements.preds"      "com.rulemaking.requirements.probs"     "com.rulemaking.requirements.preds"    
#[19] "com.time.limit.probs"                  "com.time.limit.preds"                  "delegation.probs"                     
#[22] "delegation.predictions"         

attach(data)



#appeals.index = which(com.appeals.procedures.preds == 1)
consultation.index = which(com.consultation.requirements.preds == 1)
executive.action.possible.index = which(com.executive.action.possible.preds == 1)
executive.action.req.index = which(com.executive.action.required.preds == 1)
reporting.index = which(com.reporting.requirements.preds == 1)
rulemaking.index = which(com.rulemaking.requirements.preds == 1)
#spending.index = which(com.spending.limits.preds == 1)
timelimit.index = which(com.time.limit.preds == 1)
delegation.index = which(delegation.predictions == 1)


#appeals.pcr = com.appeals.procedures.probs[appeals.index]
consultation.pcr =  com.consultation.requirements.probs[consultation.index]
executive.action.pcr =  com.executive.action.possible.probs[executive.action.index]
executive.action.req.pcr =  com.executive.action.required.probs[executive.action.req.index]
reporting.pcr =  com.reporting.requirements.probs[reporting.index]
rulemaking.pcr =  com.rulemaking.requirements.probs[rulemaking.index]
#spending.pcr =  com.spending.limits.probs[spending.index]
timelimit.pcr =  com.time.limit.probs[timelimit.index]
delegation.pdr = delegation.probs[delegation.index]

#### Now let's estimate a bunch of models
#y1 = newprovision.year[appeals.index]
y2 = newprovision.year[consultation.index]
y3 = newprovision.year[executive.action.possible.index]
y4 = newprovision.year[executive.action.req.index]
y5 = newprovision.year[reporting.index]
y6 = newprovision.year[rulemaking.index]
#y7 = newprovision.year[spending.index]
y8 = newprovision.year[timelimit.index]
y9 = newprovision.year[delegation.index]


#d1 = RDDdata(y = appeals.pcr, x = y1, cutpoint = 1993.5)
d2 = RDDdata(y = consultation.pcr, x =y2, cutpoint = 1993.5)
d3 = RDDdata(y = executive.action.pcr, x =y3, cutpoint = 1993.5)
d4 = RDDdata(y = executive.action.req.pcr, x =y4, cutpoint = 1993.5)
d5 = RDDdata(y = reporting.pcr, x =y5, cutpoint = 1993.5)
d6 = RDDdata(y = rulemaking.pcr, x =y6, cutpoint = 1993.5)
#d7 = RDDdata(y = spending.pcr, x =y7, cutpoint = 1993.5)
d8 = RDDdata(y = timelimit.pcr, x =y8, cutpoint = 1993.5)
d9 = RDDdata(y = delegation.pdr, x = y9 , cutpoint = 1993.5)

#bw_ik1 = RDDbw_IK(d1)
bw_ik2 = RDDbw_IK(d2)
bw_ik3 = RDDbw_IK(d3)
bw_ik4 = RDDbw_IK(d4)
bw_ik5 = RDDbw_IK(d5)
bw_ik6 = RDDbw_IK(d6)
#bw_ik7 = RDDbw_IK(d7)
bw_ik8 = RDDbw_IK(d8)
bw_ik9 = RDDbw_IK(d9)

#r1 = RDDreg_lm(RDDobject = d1, bw = bw_ik1,order=2)
r2 = RDDreg_lm(RDDobject = d2, bw = bw_ik2,order=2)
r3 = RDDreg_lm(RDDobject = d3, bw = bw_ik3,order=2)
r4 = RDDreg_lm(RDDobject = d4, bw = bw_ik4,order=2)
r5 = RDDreg_lm(RDDobject = d5, bw = bw_ik5,order=2)
r6 = RDDreg_lm(RDDobject = d6, bw = bw_ik6,order=2)
#r7 = RDDreg_lm(RDDobject = d7, bw = bw_ik7,order=2)
r8 = RDDreg_lm(RDDobject = d8, bw = bw_ik8,order=2)
r9 = RDDreg_lm(RDDobject = d9, bw = bw_ik9,order=2)

## Finally let's plot all of them and save them

setwd("~/Dropbox/Research/Papers/Delegation-ML-Project/Draft-EU-Paper/figs/APSR_RnR")

#png("pcr1_FINAL_com.png")
#plot(r1, xlab = "Year", ylab = "Mean Constraint Probability (Appeals)", ylim = c(0.50,1))
#abline(v = 1993.5,lty="dotted")
#dev.off()

png("pcr2_FINAL_com.png")
plot(r2, xlab = "Year", ylab = "Mean Constraint Probability (Consultation)", ylim = c(0.50,1))
abline(v = 1993.5,lty="dotted")
dev.off()

png("pcr3_FINAL_com.png")
plot(r3, xlab = "Year", ylab = "Probabilistic Constraint Ratio (Executive Action Possible)", ylim = c(0.50,1))
abline(v = 1993.5,lty="dotted")
dev.off()

png("pcr4_FINAL_com.png")
plot(r4, xlab = "Year", ylab = "Mean Constraint Probability (Executive Action Required)", ylim = c(0.50,1))
abline(v = 1993.5,lty="dotted")
dev.off()

png("pcr5_FINAL_com.png")
plot(r5, xlab = "Year", ylab = "Mean Constraint Probability (Reporting)", ylim = c(0.50,1))
abline(v = 1993.5,lty="dotted")
dev.off()

png("pcr6_FINAL_com.png")
plot(r6, xlab = "Year", ylab = "Mean Constraint Probability (Rulemaking)", ylim = c(0.50,1))
abline(v = 1992.5,lty="dotted")
dev.off()

#png("pcr7_FINAL.png")
#plot(r7, xlab = "Year", ylab = "Mean Constraint Probability (Spending)", ylim = c(0.50,1))
#abline(v = 1993.5,lty="dotted")
#dev.off()

png("pcr8_FINAL_com.png")
plot(r8, xlab = "Year", ylab = "Mean Constraint Probability (Time Limits)", ylim = c(0.50,1))
abline(v = 1993.5,lty="dotted")
dev.off()

png("pdr_FINAL_com.png")
plot(r9, xlab = "Year", ylab = "Mean Delegation Probability (Authority)", ylim = c(0.50,1))
abline(v = 1993.5,lty="dotted")
dev.off()




###################################

#[1] "X"                                     "newprovision.celex"                    "newprovisiondata.month"               
#[4] "newprovision.year"                     "com.consultation.requirements.probs"   "com.consultation.requirements.preds"  
#[7] "com.executive.action.possible.probs"   "com.executive.action.possible.preds"   "com.executive.action.required.probs"  
#[10] "com.executive.action.required.preds"   "com.legislative.action.possible.probs" "com.legislative.action.possible.preds"
#[13] "com.public.hearings.probs"             "com.public.hearings.preds"             "com.reporting.requirements.probs"     
#[16] "com.reporting.requirements.preds"      "com.rulemaking.requirements.probs"     "com.rulemaking.requirements.preds"    
#[19] "com.time.limit.probs"                  "com.time.limit.preds"                  "delegation.probs"                     
#[22] "delegation.predictions"         
# get the amending dummy 

amenddata = read.csv("~/Dropbox/Research/Papers/Delegation-ML-Project/FINAL-OUTPUT-DATA/APSR_RnR/final_delegation_data_rnr_amending.csv" )

amendingdummy = amenddata$amendingdummy

rm(amenddata)

public.index.amending = which(com.public.hearings.preds == 1&amendingdummy == 1)
consultation.index.amending = which(com.consultation.requirements.preds == 1&amendingdummy == 1)
executive.action.possible.index.amending = which(com.executive.action.possible.preds == 1&amendingdummy == 1)
executive.action.req.index.amending = which(com.executive.action.required.preds == 1&amendingdummy == 1)
reporting.index.amending = which(com.reporting.requirements.preds == 1&amendingdummy == 1)
rulemaking.index.amending = which(com.rulemaking.requirements.preds == 1&amendingdummy == 1)
legislative.index.amending = which(com.legislative.action.possible.preds == 1&amendingdummy == 1)
timelimit.index.amending = which(com.time.limit.preds == 1&amendingdummy == 1)
delegation.index.amending = which(delegation.predictions == 1&amendingdummy == 1)


public.index.noamending = which(com.public.hearings.preds == 1&amendingdummy == 0)
consultation.index.noamending = which(com.consultation.requirements.preds == 1&amendingdummy == 0)
executive.action.possible.index.noamending = which(com.executive.action.possible.preds == 1&amendingdummy == 0)
executive.action.req.index.noamending = which(com.executive.action.required.preds == 1&amendingdummy == 0)
reporting.index.noamending = which(com.reporting.requirements.preds == 1&amendingdummy == 0)
rulemaking.index.noamending = which(com.rulemaking.requirements.preds == 1&amendingdummy == 0)
legislative.index.noamending = which(com.legislative.action.possible.preds == 1&amendingdummy == 0)
timelimit.index.noamending = which(com.time.limit.preds == 1&amendingdummy == 0)
delegation.index.noamending = which(delegation.predictions == 1&amendingdummy == 0)

# Create amending v non-amending probabilities
#amending
public.pcr.amending = com.public.hearings.probs[public.index.amending]
consultation.pcr.amending = com.consultation.requirements.probs[consultation.index.amending]
executive.action.possible.pcr.amending = com.executive.action.possible.probs[executive.action.possible.index.amending]
executive.action.req.pcr.amending = com.executive.action.required.probs[executive.action.req.index.amending]
reporting.pcr.amending = com.reporting.requirements.probs[reporting.index.amending]
rulemaking.pcr.amending = com.rulemaking.requirements.probs[rulemaking.index.amending]
legislative.pcr.amending = com.legislative.action.possible.probs[legislative.index.amending]
timelimit.pcr.amending = com.time.limit.probs[timelimit.index.amending]
delegation.pdr.amending = delegation.probs[delegation.index.amending]

#no amending
public.pcr.noamending = com.public.hearings.probs[public.index.noamending]
consultation.pcr.noamending = com.consultation.requirements.probs[consultation.index.noamending]
executive.action.possible.pcr.noamending = com.executive.action.possible.probs[executive.action.possible.index.noamending]
executive.action.req.pcr.noamending = com.executive.action.required.probs[executive.action.req.index.noamending]
reporting.pcr.noamending = com.reporting.requirements.probs[reporting.index.noamending]
rulemaking.pcr.noamending = com.rulemaking.requirements.probs[rulemaking.index.noamending]
legislative.pcr.noamending = com.legislative.action.possible.probs[legislative.index.noamending]
timelimit.pcr.noamending = com.time.limit.probs[timelimit.index.noamending]
delegation.pdr.noamending = delegation.probs[delegation.index.noamending]

t1 = t.test(delegation.pdr.amending,delegation.pdr.noamending)
t2 = t.test(timelimit.pcr.amending,timelimit.pcr.noamending)
t3 = t.test(public.pcr.amending,public.pcr.noamending)
t4 = t.test(rulemaking.pcr.amending,rulemaking.pcr.noamending)
t5 = t.test(reporting.pcr.amending,reporting.pcr.noamending)
t6 = t.test(executive.action.possible.pcr.amending,executive.action.possible.pcr.noamending)
t7 = t.test(executive.action.req.pcr.amending,executive.action.req.pcr.noamending)
t8 = t.test(consultation.pcr.amending,consultation.pcr.noamending)
t9 = t.test(legislative.pcr.amending,legislative.pcr.noamending)



dfreedom = c(t1$parameter,
             t2$parameter,
             t3$parameter,
             t4$parameter,
             t5$parameter,
             t6$parameter,
             t7$parameter,
             t8$parameter,
             t9$parameter
             )

dfreedom = as.vector(round(dfreedom,digits = 0))

amendingest = c(t1$estimate[1],
                t2$estimate[1],
                t3$estimate[1],
                t4$estimate[1],
                t5$estimate[1],
                t6$estimate[1],
                t7$estimate[1],
                t8$estimate[1],
                t9$estimate[1]
)

noamendingest = c(t1$estimate[2],
                  t2$estimate[2],
                  t3$estimate[2],
                  t4$estimate[2],
                  t5$estimate[2],
                  t6$estimate[2],
                  t7$estimate[2],
                  t8$estimate[2],
                  t9$estimate[2]
)

diff = noamendingest-amendingest

pvalues = c(t1$p.value,
            t2$p.value,
            t3$p.value,
            t4$p.value,
            t5$p.value,
            t6$p.value,
            t7$p.value,
            t8$p.value,
            t9$p.value
)


library(xtable)

amendingcompare = data.frame(noamendingest,amendingest,diff,pvalues,dfreedom)

names(amendingcompare) = c("Non-Amending", "Amending", "Difference", "P Value","Degrees of Freedom")

rownames(amendingcompare) = c("Mean Pr(Delegation)",
                              "Mean Pr(Time Limit Constraint)",
                              "Mean Pr(Public Hearings)",
                              "Mean Pr(Rulemaking)",
                              "Mean Pr(Reporting Constraint)",
                              "Mean Pr(Exec. Action Possible)",
                              "Mean Pr(Exec. Action Req Constraint)",
                              "Mean Pr(Cons. Constraint)",
                              "Mean Pr(Leg. Act. Constraint)"
)


digits(amendingcompare)<-c(0,3,3,3,3,0)

xtable(amendingcompare,digits = c(0,3,3,3,3,0))


############################################################################################################################################
############################################################################################################################################
############################################################################################################################################
############################################################################################################################################
## Plots of predicted constraint v actual in Franchino
franchino.ratio<-
  read.csv("~/Dropbox/Research/Papers/Delegation-ML-Project/Data/delegationfran.csv")
###################################

























